What is the purpose of the .each() function in jQuery? Explain with an example.
What is the purpose of the .each() function in jQuery? Explain with an example.
323
14-Apr-2023
Updated on 12-Jun-2023
Aryan Kumar
12-Jun-2023The jQuery .each() function is used to iterate over a jQuery object, executing a function for each matched element. The function is passed two arguments: the current element and its index.
Here is an example of how to use the .each() function to change the color of all the even-numbered li elements in a list:
Code snippet
This code will iterate over all the li elements in the document and call the function for each one. The function will check the index of the current element. If the index is even, the function will change the color of the element to red.
The .each() function is a powerful tool that can be used to perform a variety of tasks on a jQuery object. It is a versatile function that can be used in a variety of situations.
Here are some other examples of how to use the .each() function:
Code snippet
Code snippet
Code snippet
The .each() function is a powerful tool that can be used to perform a variety of tasks on a jQuery object, an array, or an object. It is a versatile function that can be used in a variety of situations.